package com.ljx.rabbit.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.rabbit.domain.po.Order;
import com.ljx.rabbit.mapper.OrderMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Component
@Slf4j
@RequiredArgsConstructor
public class OrderTask {
    private final OrderMapper orderMapper;

    /**
     * 处理支付超时订单
     */
    @Scheduled(cron = "* 0/5 * * * ?")
    public void processTimeoutOrder(){
        log.info("处理支付超时订单：{}", new Date());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.lambda().le(Order::getPayEnd, LocalDateTime.now()).eq(Order::getOrderState, 1);
        orderMapper.update(new Order().setOrderState(6),wrapper);
    }

    /**
     * 处理待发货
     */
    @Scheduled(cron = "* 0/30 * * * ?")
    public void processDeliverOrder(){
        log.info("处理待发货订单：{}", new Date());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.lambda().le(Order::getPayTime, LocalDateTime.now().minusHours(1)).eq(Order::getOrderState, 2);
        orderMapper.update(new Order().setOrderState(3),wrapper);
    }

    /**
     * 待收货
     */
    @Scheduled(cron = "* 0/30 * * * ?")
    public void processReceiveOrder(){
        log.info("处理待收货订单：{}", new Date());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.lambda().le(Order::getPayTime, LocalDateTime.now().minusHours(2)).eq(Order::getOrderState, 3);
        orderMapper.update(new Order().setOrderState(4),wrapper);
    }
    /**
     * 待评价
     */
    @Scheduled(cron = "* 0/30 * * * ?")
    public void processEvaluationOrder(){
        log.info("处理待评价订单：{}", new Date());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.lambda().le(Order::getPayTime, LocalDateTime.now().minusHours(5)).eq(Order::getOrderState, 4);
        orderMapper.update(new Order().setOrderState(7),wrapper);
    }

}
